package com.example.project.Controller;


import com.example.project.Entity.Bill;
import com.example.project.Service.BillService;
import com.example.project.Service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("/Bill")
public class BillController {
    @Autowired
    BillService billService;


    @RequestMapping("/byConditions")
    public String GetBillsByConditions(Model model,
                                       @RequestParam(defaultValue = "1") Integer pageNum,
                                       @RequestParam(defaultValue = "5") Integer pageSize,
                                       @RequestParam(required = false) int typeid,
                                       @RequestParam(required = false) String title,
                                       @RequestParam(required = false) String remark,
                                       @RequestParam(required = false) String billtime) {
        Bill conditions = new Bill();
        conditions.setTypeid(typeid);
        conditions.setTitle(title);
        conditions.setRemark(remark);
        if (billtime != null && !billtime.trim().isEmpty() && !billtime.equals("null")) {
            LocalDate parsedBilltime = LocalDate.parse(billtime);
            conditions.setBilltime(parsedBilltime);
        }
        List<Bill> bills = billService.QueryByConditions(conditions,pageNum,pageSize);
        PageInfo<Bill> pageInfo = new PageInfo<>(bills);
        model.addAttribute("conditions", conditions);
        model.addAttribute("pageInfo", pageInfo);
        return "list";
    }
    @RequestMapping("/addBill")
    public String AddBill(Bill bill, Model model) {
        billService.addBill(bill);
        int newId = bill.getId();
        Bill bill1 = billService.queryById(bill);
        PageInfo<Bill> addPageInfo = new PageInfo<>(Collections.singletonList(bill1));
        // 将伪装的 PageInfo 对象传递给视图
        model.addAttribute("pageInfo", addPageInfo);
        model.addAttribute("conditions", bill1);
        return "list";
    }

    @RequestMapping("/editBill")
    public String EditBill(Bill bill,Model model) {
        billService.updateBill(bill);
        Bill bill2 = billService.queryById(bill);
        Bill bill3=new Bill();
        PageInfo<Bill> editedPageInfo = new PageInfo<>(Collections.singletonList(bill2));
        model.addAttribute("pageInfo", editedPageInfo);
        model.addAttribute("conditions", bill3);
        return "list";
    }

    @RequestMapping("/deleteBill")
    public String DeleteBill(int id, RedirectAttributes attributes) {
        billService.deleteBill(id);
        String redirectUrl = "redirect:/Bill/byConditions?pageNum=1&typeid=0&title=&remark=&billtime=null";
        attributes.addFlashAttribute("message", "Bill deleted successfully");

        return redirectUrl;
    }

    @RequestMapping("/querybyAll")
    public String queryByAll(Model model) {
        List<Bill> bills = billService.QueryByAll();
        model.addAttribute("bills", bills);
        System.out.println(bills);
        return "list";
    }
}
